package javax.xml.bind.annotation.adapters;

public final class NormalizedStringAdapter extends XmlAdapter<String, String> {
    public String marshal(String s) {
        return s;
    }

    public String unmarshal(String text) {
        if (text == null) {
            return null;
        }

        int index = text.length() - 1;

        while ((index >= 0) && !nonSpaceWhite(text.charAt(index)))
            index--;

        if (index < 0) {
            return text;
        }

        char[] buf = text.toCharArray();

        buf[index--] = ' ';

        for (; index >= 0; index--)
            if (nonSpaceWhite(buf[index])) {
                buf[index] = ' ';
            }

        return new String(buf);
    }

    protected static boolean nonSpaceWhite(char ch) {
        if (ch >= 0x20) {
            return false;
        }

        return (ch == 0x9) || (ch == 0xA) || (ch == 0xD);
    }
}
